import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Random;

/**
 * @author sidyous
 */

public class TestStep1
{

	public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException
	{
		test1(args);
	}
	
	/**
	 * Test Step1 
	 */
	public static void test1(String[] args) throws RemoteException, MalformedURLException, NotBoundException
	{	
		int nb_iter = 100000;	
		double frequency_wr = Double.parseDouble(args[0]);
		Random rnd = new Random();
		
		// initialize the system
		Client.init();
		
		// look up or create an object
		sidyous_itf s = (sidyous_itf) Client.lookup("sidyous");
		if (s == null)
		{
			s = (sidyous_itf) Client.create(new sidyous(0));
			Client.register("sidyous", s);
		}
		
		// ATTACK !!!
		int nbw = 0;
		for (int i = 0; i < nb_iter; i++)
		{
			if (rnd.nextDouble() < frequency_wr)
			{
				//write
				s.lock_write();
				s.setS(((sidyous_itf)s).getS() + 1);
				nbw++;
				System.out.println("obj = " + (sidyous_itf)s);
				s.unlock();
			}
			else
			{
				//read
				s.lock_read();
				System.out.println(((sidyous_itf)s).getS());
				s.unlock();
			}
		}
		s.lock_read();
		System.out.println("Last value: "+((sidyous_itf)s).getS());
		System.out.println("nb write = " + nbw);
		s.unlock();
	}
}
